<?php
 
 
function aSearchPrice(){
    return array(
        0 => array(
            'name' => 'Thỏa thuận' ,
            'value' => '0' ,
            'min_price' => 0 ,
            'max_price' => 0
        ),
        1 => array(
            'name' => '< 500 Triệu' ,
            'value' => '1' ,
            'min_price' => 0 ,
            'max_price' => 500000000
        ),
        2 => array(
            'name' => '500 - 800 Triệu' ,
            'value' => '2' ,
            'min_price' => 500000000 ,
            'max_price' => 800000000
        ),		
        3 => array(
            'name' => '800 Triệu - 1 tỷ' ,
            'value' => '3' ,
            'min_price' => 800000000 ,
            'max_price' => 1000000000
        ),
        4 => array(
            'name' => '1 tỷ - 2 tỷ' ,
            'value' => '4' ,
            'min_price' => 1000000000 ,
            'max_price' => 2000000000
        ),
        5 => array(
            'name' => '2 tỷ - 3 tỷ' ,
            'value' => '5' ,
            'min_price' => 2000000000 ,
            'max_price' => 3000000000
        ),
        6 => array(
            'name' => '3 tỷ - 5 tỷ' ,
            'value' => '6' ,
            'min_price' => 3000000000 ,
            'max_price' => 5000000000
        ),
        7 => array(
            'name' => '5 tỷ - 7 tỷ' ,
            'value' => '7' ,
            'min_price' => 5000000000 ,
            'max_price' => 7000000000
        ),
        8 => array(
            'name' => '7 tỷ - 10 tỷ' ,
            'value' => '8' ,
            'min_price' => 7000000000 ,
            'max_price' => 10000000000
        ),
        9 => array(
            'name' => '10 tỷ - 20 tỷ' ,
            'value' => '9' ,
            'min_price' => 10000000000 ,
            'max_price' => 20000000000
        ),
        10 => array(
            'name' => '20 - 30 tỷ' ,
            'value' => '10' ,
            'min_price' => 20000000000 ,
            'max_price' => 30000000000
        ),
        11 => array(
            'name' => '> 30 tỷ ' ,
            'value' => '11' ,
            'min_price' => 30000000000 ,
            'max_price' => 9999999999999
        ),
        12 => array(
            'name' => '< 1 triệu' ,
            'value' => '12' ,
            'min_price' => 0 ,
            'max_price' => 1000000
        ),
        13 => array(
            'name' => '1 - 3 triệu' ,
            'value' => '13' ,
            'min_price' => 1000000 ,
            'max_price' => 3000000
        ),
        14 => array(
            'name' => '3 - 5 triệu' ,
            'value' => '14' ,
            'min_price' => 3000000 ,
            'max_price' => 5000000
        ),
        15 => array(
            'name' => '5 - 10 triệu' ,
            'value' => '15' ,
            'min_price' => 5000000 ,
            'max_price' => 10000000
        ),
        16 => array(
            'name' => '10 - 40 triệu' ,
            'value' => '16' ,
            'min_price' => 10000000 ,
            'max_price' => 40000000
        ),
        17 => array(
            'name' => '40 - 70 triệu' ,
            'value' => '17' ,
            'min_price' => 40000000 ,
            'max_price' => 70000000
        ),
        18 => array(
            'name' => '70 - 100 triệu' ,
            'value' => '18' ,
            'min_price' => 70000000 ,
            'max_price' => 100000000
        ),
        19 => array(
           'name' => '> 100 triệu' ,
           'value' => '19' ,
           'min_price' => 100000000 ,
           'max_price' => 9999999999999
        )
    );
}

function aSearchRentPrice(){
	return array(
		12 => array(
			'name' => '< 1 triệu' ,
			'value' => '12' ,
			'min_price' => 0 ,
			'max_price' => 1000000
		),
		13 => array(
			'name' => '1 - 3 triệu' ,
			'value' => '13' ,
			'min_price' => 1000000 ,
			'max_price' => 3000000
		),
		14 => array(
			'name' => '3 - 5 triệu' ,
			'value' => '14' ,
			'min_price' => 3000000 ,
			'max_price' => 5000000
		),
		15 => array(
			'name' => '5 - 10 triệu' ,
			'value' => '15' ,
			'min_price' => 5000000 ,
			'max_price' => 10000000
		),
		16 => array(
			'name' => '10 - 40 triệu' ,
			'value' => '16' ,
			'min_price' => 10000000 ,
			'max_price' => 40000000
		),
		17 => array(
			'name' => '40 - 70 triệu' ,
			'value' => '17' ,
			'min_price' => 40000000 ,
			'max_price' => 70000000
		),
		18 => array(
			'name' => '70 - 100 triệu' ,
			'value' => '18' ,
			'min_price' => 70000000 ,
			'max_price' => 100000000
		),
		19 => array(
		   'name' => '> 100 triệu' ,
		   'value' => '19' ,
		   'min_price' => 100000000 ,
		   'max_price' => 9999999999999
		),
	);
}

function aSearchSalePrice(){
    return array(
        0 => array(
            'name' => 'Thỏa thuận' ,
            'value' => '0' ,
            'min_price' => 0 ,
            'max_price' => 0
        ),
        1 => array(
            'name' => '< 500 Triệu' ,
            'value' => '1' ,
            'min_price' => 0 ,
            'max_price' => 500000000
        ),
        2 => array(
            'name' => '500 - 800 Triệu' ,
            'value' => '2' ,
            'min_price' => 500000000 ,
            'max_price' => 800000000
        ),		
        3 => array(
            'name' => '800 Triệu - 1 tỷ' ,
            'value' => '3' ,
            'min_price' => 800000000 ,
            'max_price' => 1000000000
        ),
        4 => array(
            'name' => '1 tỷ - 2 tỷ' ,
            'value' => '4' ,
            'min_price' => 1000000000 ,
            'max_price' => 2000000000
        ),
        5 => array(
            'name' => '2 tỷ - 3 tỷ' ,
            'value' => '5' ,
            'min_price' => 2000000000 ,
            'max_price' => 3000000000
        ),
        6 => array(
            'name' => '3 tỷ - 5 tỷ' ,
            'value' => '6' ,
            'min_price' => 3000000000 ,
            'max_price' => 5000000000
        ),
        7 => array(
            'name' => '5 tỷ - 7 tỷ' ,
            'value' => '7' ,
            'min_price' => 5000000000 ,
            'max_price' => 7000000000
        ),
        8 => array(
            'name' => '7 tỷ - 10 tỷ' ,
            'value' => '8' ,
            'min_price' => 7000000000 ,
            'max_price' => 10000000000
        ),
        9 => array(
            'name' => '10 tỷ - 20 tỷ' ,
            'value' => '9' ,
            'min_price' => 10000000000 ,
            'max_price' => 20000000000
        ),
        10 => array(
            'name' => '20 - 30 tỷ' ,
            'value' => '10' ,
            'min_price' => 20000000000 ,
            'max_price' => 30000000000
        ),
        11 => array(
            'name' => '> 30 tỷ ' ,
            'value' => '11' ,
            'min_price' => 30000000000 ,
            'max_price' => 9999999999999
        ),       
    );
}


function findPrice( $value ){
	$aPrice = aSearchPrice();
	foreach ($aPrice as $item){
		if($item['value'] == $value){
			return $item;
		}
	}
	return '';
}

function findPrice2( $value ){
    $aPrice = aSearchPrice();
	foreach ($aPrice as $item){
		if($item['min_price'] < $value && $item['max_price'] >= $value){
			return $item;
		}
	}
	return 0;
}

function mdGetListPriceByTypePost( $type_post ){
	if($type_post == 1){//cho thue
		return aSearchRentPrice();		
	}
	elseif($type_post == 2){//ban
		return aSearchSalePrice();
	}
	return NULL;
}


function findArea2($value){
    $aArea = aSearchArea();
	foreach ($aArea as $item){
		if($item['value'] == $value){
			return $item;
		}
	}
	return 0;
}

function findArea($value){
    $aArea = aSearchArea();
	foreach ($aArea as $item){
		if($item['min_area'] < $value && $item['max_area'] >= $value){
			return $item;
		}
	}
	return 0;
}
function aSearchArea(){
    return array(
	0 => array(
		'name' => 'Không xác định' ,
		'value' => '0' ,
		'min_area' => 0 ,
		'max_area' => 0
	),
	1 => array(
		'name' => '<= 30 m²' ,
		'value' => '1' ,
		'min_area' => 0 ,
		'max_area' => 30 
	),
	2 => array(
		'name' => '30 - 50 m²' ,
		'value' => '2' ,
		'min_area' => 30 ,
		'max_area' => 50 
	),		
	3 => array(
		'name' => '50 - 80 m²' ,
		'value' => '3' ,
		'min_area' => 50 ,
		'max_area' => 80 
	),
	4 => array(
		'name' => '80 - 100 m²' ,
		'value' => '4' ,
		'min_area' => 80 ,
		'max_area' => 100 
	),
	5 => array(
		'name' => '100 - 150 m²' ,
		'value' => '5' ,
		'min_area' => 100 ,
		'max_area' => 150 
	),
	6 => array(
		'name' => '150 - 200 m²' ,
		'value' => '6' ,
		'min_area' => 150 ,
		'max_area' => 200 
	),
	7 => array(
		'name' => '200-250 m²' ,
		'value' => '7' ,
		'min_area' => 250 ,
		'max_area' => 300 
	),
	8 => array(
		'name' => '250 - 300 m²' ,
		'value' => '8' ,
		'min_area' => 200 ,
		'max_area' => 250 
	),
	9 => array(
		'name' => '300 - 500 m²' ,
		'value' => '9' ,
		'min_area' => 300 ,
		'max_area' => 350 
	),
	10 => array(
		'name' => '>=500 m²' ,
		'value' => '10' ,
		'min_area' => 500 ,
		'max_area' => 99999999999999999 
	),
    );
}

function arraySearchNumber(){
	return array(
		1 => array(
			'name' => '+1' ,
			'value' => '1' ,
			
		),
		2 => array(
			'name' => '+2' ,
			'value' => '2' ,			
		),		
		3 => array(
			'name' => '+3' ,
			'value' => '3' ,			
		),
		4 => array(
			'name' => '+4' ,
			'value' => '4' ,		
		),
		5 => array(
			'name' => '+5' ,
			'value' => '5' ,		
		),
	);
}


function checkAndInsertAliasPathRealEstate( $link , $alias){ 
	$r_link = drupal_get_path_alias( $link );
	if( $alias != $r_link ){
		/* Insert alias for realestate */		
		  $alias_path = array(
			'pid' => NULL,
			'language' => LANGUAGE_NONE,
			'source' =>  $link ,
			'alias' => $alias
		  );
		  path_save( $alias_path );
		 /*---------------------------------------*/
		if (isset($alias_path)) {
            $redirect = new stdClass();
            redirect_object_prepare($redirect);
            $redirect->source =  $link;
            $redirect->redirect = BASE_URL . $alias ;
            redirect_save($redirect);
        }
       // $r_link = $alias;
        return BASE_URL . $alias;
		 /* Insert info contact for realestate */
		
		 /*----------------------------------------*/    
	}
	else{
		return BASE_URL . $r_link;
	}
	return BASE_URL . $r_link;
}

function get_start_pagination($current , $total , $limit){ 
    if($total <= $limit){
        return array(
            'start' => 1 ,
            'end' => $total
        );
    }
    elseif($total > $limit){
        if($current <= 2){
            return array(
                'start' => 1 ,
                'end' => $limit
            );
        }
        elseif($current > 2){
            if($current + 2 >= $total){ 
                return array(
                    'start' => $current - 2 ,
                    'end' => $total
                );
            }
            else{
                return array(
                    'start' => $current - 2 ,
                    'end' => $current + 2
                );
            }
        }
    }
}

